Airpodder.data.ChannelStore = Ext.extend(Ext.data.Store, {
  constructor : function(){
    Airpodder.data.ChannelStore.superclass.constructor.call(this, {
      sortInfo  : {field: 'title', direction: "ASC"},
      reader    : new Ext.data.JsonReader({
        id      : 'id',
        fields  : Airpodder.data.Channel
      })
    });
    this.conn  = Airpodder.data.conn;
    this.proxy = new Ext.sql.Proxy(this.conn, 'channel', 'id', this);
    this.on('feedloaded', this.updateChannel);
    this.addEvents(
      "channelupdated"
    );    
  },

  getId    : function(id) {
    var c = this.data.map[id];
    return c ? c.data.id : '';
  },

  getTitle    : function(id) {
    var c = this.data.map[id];
    return c ? c.data.title : '';
  },

  getDescription    : function(id) {
    var c = this.data.map[id];
    return c ? c.data.description : '';
  },
  
  updateChannel: function(url, res) {
    var channel = this.addChannel(url, res);
    return this.fireEvent('channelupdated', this.getId(channel.data.id), res.items);
  },

  addChannel: function (url, res) {
    return this._addChannel(url, res.title, res.link, res.description, res.image, '', '', '', '');
  },

  _addChannel : function (url, title, link, description, image, username, password, etag, foldername) {
    var c = this.findChannel(url);
    if (!c) {
      var id = Ext.uniqueId();
      c = new Airpodder.data.Channel({
        id                    : id,
        url                   : url,
        title                 : title,
        override_title        : title,
        link                  : link,
        description           : description,
        image                 : image,
        pubDate               : new Date(),
        sync_to_devices       : 1,
        device_playlist_name  : '',
        username              : username,
        password              : password,
        last_modified         : '',
        etag                  : etag,
        deleted               : 0,
        channel_is_locked     : 0,
        foldername            : foldername,
        auto_foldername       : 1
      }, id);
      this.add(c);
    }
    return c;
  },

  findChannel : function (url) {
    var d = this.data;
    for (var i = 0, len = d.length; i < len; i++) {
      if (d.items[i].data.url === url) {
        return d.items[i];
      }
    }
    return null;
  },

  loadDemoChannels : function () {
    this.addChannel(
      'http://feeds.rucast.net/Umputun',
      'UWP - Eженедельный подкаст от Umputun',
      'http://podcast.umputun.com',
      'Еженедельный подкаст от Umputun (US, Чикаго)',
      '1.jpg',
      '',
      '',
      '',
      'UWP - Eженедельный подкаст от Umputun'
    );
    this.addChannel(
      'http://feeds.rucast.net/radio-t',
      'Радио-Т',
      'http://radio-t.com',
      'Импровизации на Hi-Tech темы',
      '2.jpg',
      '',
      '',
      '',
      'Радио-Т'
    );
  },

  prepareTable : function() {
    try {
      this.conn.createTable({
        name    : 'channel',
        key     : 'id',
        fields  : Airpodder.data.Channel.prototype.fields
      });
    } catch(e) {
      c_log(e);
    }
  }
});

